package servlet;

import dao.DaoFactory;
import entity.Exam;
import entity.Student;
import utils.PathUtils;
import utils.UploadUtil;
import jxl.Cell;
import jxl.Workbook;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
//读Xlsx文件将学生添加进数据库
@WebServlet("/readXlsx")
public class ReadXlsxServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Exam exam = (Exam) req.getSession().getAttribute("exam");
        Integer e_id = exam.getE_id();
        String newFileName = null;
        try {
            // 以下三行代码功能: 通过request.getInputStream();获取到请求体的全部内容
            // 进行解析,将每对分割线中的内容封装在了FileItem对象中
            DiskFileItemFactory fac = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(fac);
            List<FileItem> list = upload.parseRequest(req);
            // 4_遍历集合
            for (FileItem item : list) {
                if (item.isFormField()) {
                    continue;
                } else {

                    // 获取到原始的文件名称
                    String oldFileName = item.getName();

                    newFileName = UploadUtil.getUUIDName(oldFileName);//设置随机文件名

                    InputStream is = item.getInputStream();

                    String realPath = getServletContext().getRealPath("/studentXlsx/");

                    File dirFile = new File(realPath);
                    if (!dirFile.exists()) {
                        dirFile.mkdirs();
                    }
                    // 在服务端创建一个空文件(后缀必须和上传到服务端的文件名后缀一致)
                    File file = new File(realPath, newFileName);
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    // 建立和空文件对应的输出流
                    OutputStream os = new FileOutputStream(file);
                    // 将输入流中的数据刷到输出流中

                    // 释放资源
                    IOUtils.copy(is, os);
                    IOUtils.closeQuietly(is);
                    IOUtils.closeQuietly(os);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        // 读表格

        try {

            Workbook wb = Workbook.getWorkbook(new File(getServletContext().getRealPath("/studentXlsx/") + newFileName));

            for (int i = 1; i < wb.getSheet(0).getRows(); i++) {

                Cell[] cell = wb.getSheet(0).getRow(i);
                Student student = new Student();

                //添加学生
                student.setE_id(e_id);
                student.setS_id(cell[0].getContents());
                student.setS_name(cell[1].getContents());
                student.setS_class(cell[2].getContents());
                try {
                    DaoFactory.getInstance().getStudentDao().add(student);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            //直接重定向到列表页面
            resp.sendRedirect(PathUtils.getBasePath(req)+"exam?method=listStudent&msg=Add student sucessful!");

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
